<template>
  <div class="inventory">
    <el-card>
      <div class="header" slot="header">库存列表</div>
      <div class="card-body">
        <!-- 条件查询 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          :model="searchData"
          ref="searchFormData"
        >
          <el-form-item label="商品名称">
            <el-input
              placeholder="商品名称"
              v-model="searchData.product_name"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
        <!-- 库存表格 -->
        <el-table
          :data="inventoryDatas"
          stripe
          :style="{ width: tableWidth + 'px' }"
        >
          <el-table-column label="商品名称" prop="product_name">
          </el-table-column>
          <el-table-column label="库存数量">
            <template slot-scope="scope">
              <span v-if="!scope.row.isEdit">{{ scope.row.quantity }}</span>
              <el-input
                v-else
                v-model="scope.row.quantity"
                placeholder="库存数据量"
                type="number"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="库存预警">
            <template slot-scope="scope">
              <el-alert
                v-if="scope.row.quantity > 1000"
                title="库存充足"
                type="success"
                :closable="false"
                effect="dark"
                center
                show-icon
              >
              </el-alert>
              <el-alert
                v-else-if="scope.row.quantity > 500"
                title="请及时补充库存"
                type="info"
                :closable="false"
                effect="dark"
                center
                show-icon
              >
              </el-alert>
              <el-alert
                v-else-if="scope.row.quantity > 100"
                title="库存不足"
                type="warning"
                :closable="false"
                effect="dark"
                center
                show-icon
              >
              </el-alert>
              <el-alert
                v-else-if="scope.row.quantity > 10"
                title="库存严重不足"
                type="error"
                :closable="false"
                effect="dark"
                center
                show-icon
              >
              </el-alert>
              <el-alert
                v-else
                title="库存已严重缺货"
                type="error"
                :closable="false"
                effect="dark"
                center
                show-icon
              >
              </el-alert>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.row)"
                :type="scope.row.isEdit ? 'success' : ''"
              >
                {{ scope.row.isEdit ? "完成" : "编辑" }}
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页条 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getInventory, addGoodsInventory } from "@/api/inventory";
export default {
  data() {
    return {
      searchData: {
        product_name: "",
      },
      inventoryDatas: [],
      currentPage: 1,
      pageSize: 5,
      total: 1,
      tableWidth: 1400,
    };
  },
  methods: {
    handleSizeChange(size) {
      this.pageSize = size;
      this.loadInventory();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.loadInventory();
    },
    // 条件查询
    onSubmit() {
      this.loadInventory();
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      if (!row.isEdit) {
        row.isEdit = true;
      } else {
        row.isEdit = false;
        // 发送ajax修改库存数据
        addGoodsInventory({
          id: row.id,
          product_id: row.product_id,
          quantity: row.quantity,
        }).then((resp) => {
          if (resp.data.code === 20000) {
            this.loadInventory();
          }
        });
      }
    },
    // 加载库存数据
    loadInventory() {
      getInventory({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchData,
      }).then((resp) => {
        let respData = resp.data;
        respData.data.forEach((v) => {
          v.isEdit = false;
        });
        this.inventoryDatas = respData.data;
        this.total = respData.total;
      });
    },
  },
  created() {
    this.loadInventory();
  },
};
</script>

<style lang="less" scoped>
.inventory {
  width: 100%;
  height: 100%;
  /deep/.el-table {
    text-align: center;
  }
  .el-pagination {
    margin-top: 20px;
  }
}
</style>
